package com.handpay.nocardpay.baselibrary.qualificationinfo.presenter;

import android.content.Context;

import com.handpay.nocardpay.baselibrary.commbase.model.IBaseModel;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorInfo;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.BankList;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.ResultSingleBankBean;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.SubBankBean;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.impl.BankModelImpl;
import com.handpay.nocardpay.baselibrary.qualificationinfo.view.IQualificationView;

import java.util.HashMap;

/**
 * Created by fplei on 2017/8/13.
 * 资质信息银行卡相关业务
 */
public class BankPresenterImpl implements BankPresenter {
    private BankModelImpl bankModel;
    private IQualificationView qualificationView;

    public BankPresenterImpl(Context context,IQualificationView mqualificationView){
        this.bankModel=new BankModelImpl(context);
        this.qualificationView=mqualificationView;
    }

    @Override
    public void queryBankList(HashMap<String, Object> paramers, boolean isShowLoading) {
        showLoading(isShowLoading);
        bankModel.queryBankList(paramers, BankList.class, new IBaseModel.OnRequestFinishedListener<BankList>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                hiddenLoading();
                if(qualificationView!=null){
                    qualificationView.queryBankListFail(baseErrorInfo.errMessage);
                }
                showError(baseErrorInfo);
            }
            @Override
            public void onSuccess(BankList response) {
                hiddenLoading();
                if(qualificationView!=null){
                    qualificationView.showBankList(response);
                }
            }
        });
    }

    @Override
    public void querySubBankList(HashMap<String, Object> paramers, boolean isShowLoading) {
        showLoading(isShowLoading);
        bankModel.querySubBankList(paramers, SubBankBean.class, new IBaseModel.OnRequestFinishedListener<SubBankBean>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                hiddenLoading();
                showError(baseErrorInfo);
            }
            @Override
            public void onSuccess(SubBankBean response) {
                if(qualificationView!=null){
                    qualificationView.showSubBankList(response);
                }
            }
        });
    }

    @Override
    public void queryBankInfoByCardBin(HashMap<String, Object> paramers, boolean isShowLoading) {
        showLoading(isShowLoading);
        bankModel.queryBankCodeByCardNo(paramers, ResultSingleBankBean.class, new IBaseModel.OnRequestFinishedListener<ResultSingleBankBean>() {
            @Override
            public void onError(BaseErrorInfo baseErrorInfo) {
                hiddenLoading();
                showError(baseErrorInfo);
            }
            @Override
            public void onSuccess(ResultSingleBankBean response) {
                hiddenLoading();
                if(qualificationView!=null){
                    qualificationView.selectedBank(response);
                }
            }
        });
    }

    private void showLoading(boolean isShowLoading){
        if (qualificationView != null) {
            if (isShowLoading) qualificationView.showProgress();
        }
    }
    private void hiddenLoading(){
        if (qualificationView != null) {
            qualificationView.hideProgress();
        }
    }
    private void showError(BaseErrorInfo baseErrorInfo){
        if(qualificationView!=null){
            qualificationView.showErrorAlert(baseErrorInfo);
        }
    }
    @Override
    public void onDestory() {
        bankModel=null;
        qualificationView=null;
    }

}
